﻿
@{
    ViewBag.Title = "过站查询";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var siteTypes = ViewBag.SiteTypes as List<KeyValuePair<int, string>>;
    var startTime = DateTime.Now.TimeOfDay > new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay > new TimeSpan(8, 0, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : DateTime.Today.AddDays(-1).AddHours(8));
    var endTime = DateTime.Now.TimeOfDay > new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(20) : (DateTime.Now.TimeOfDay > new TimeSpan(8, 0, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddDays(-1).AddHours(20));
}

@section Contents{
    <link href="~/res/kendo/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="~/res/kendo/styles/kendo.material.min.css" rel="stylesheet" />
    <style>
        .layui-form-label {
            width: auto;
        }

        #txtOrderNo, #txtWorkOrderNo {
            width: 100%;
            height: 30px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" name="workShopCode" v-html="lang.Field.WorkShop">生产车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" name="lineId" v-html="lang.Field.AssemblyLine">流水线：</label>
                        <div class="layui-input-inline">
                            <select id="selLines" lay-filter="selLines">
                                <option value="0">全部</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.SiteType">站点类型：</label>
                        <div class="layui-input-inline">
                            <select id="selSiteTypes">
                                <option value="">全部</option>
                                @foreach (var item in siteTypes)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" value="@ViewBag.OrderNo" @(!string.IsNullOrWhiteSpace(ViewBag.OrderNo) ? " disabled " : "") autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtWorkOrderNo" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.BarCode">组件条码：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtBarCode" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ViaSiteTime">过站时间：</label>
                        <div class="layui-input-inline" style="width:440px;">
                            <input type="text" id="txtRangeTime" style="width:310px;float:left;" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input">
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnPreDuty" type="button" style="margin-top:-6px;">
                                <i class="layui-icon layui-icon-prev layuiadmin-button-btn"></i>
                            </button>
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnNextDuty" type="button" style="margin-top:-6px;margin-left:0;">
                                <i class="layui-icon layui-icon-next layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <a id="btnExport" target="_blank" class="layui-btn layui-btn-normal layuiadmin-btn-list" href="javascript:void(0)">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script src="/res/kendo/kendo.all.min.js"></script>
    <script src="/res/kendo/js/cultures/kendo.culture.zh-CN.min.js"></script>
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    laydate = layui.laydate,
                    form = layui.form;
                laydate.render({
                    elem: '#txtRangeTime'
                    , type: 'datetime'
                    , range: '至'
                    , done: function (value, date, endDate) {
                        queryNos(0);
                        queryNos(1);
                    }
                });
                form.on("select(selWorkshops)", function (data) {
                    queryNos(0);
                    queryNos(1);
                    queryLines();
                });
                queryNos(0);
                queryNos(1);

                var txtOrderNos = $("#txtOrderNo").kendoAutoComplete({
                    dataSource: vm.$data.nos,
                    filter: "contains",
                    placeholder: "选择订单号"
                }).focus(function () {
                    txtOrderNos.search("");
                }).data("kendoAutoComplete"),
                    txtWorkOrderNos = $("#txtWorkOrderNo").kendoAutoComplete({
                        dataSource: vm.$data.nos,
                        filter: "contains",
                        placeholder: "选择工单号"
                    }).focus(function () {
                        txtWorkOrderNos.search("");
                    }).data("kendoAutoComplete");

                function queryLines() {
                    var loadId = layui.layer.load();
                    $.ajax({
                        url: "/Report/QueryLines",
                        data: {
                            workshopCode: $("#selWorkshops").val().trim()
                        },
                        complete: function () {
                            layui.layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            var html = ["<option value='0'>全部</option>"];
                            for (var i = 0; i < res.Data.length; i++) {
                                html.push("<option value='" + res.Data[i].PkId + "'>" + res.Data[i].LineName + "</option>");
                            }
                            $("#selLines").html(html.join(""));
                            form.render("select");
                        }
                    });
                }

                function queryNos(noType) {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0],
                        endTime = rangeTime.length <= 0 ? null : rangeTime[1];
                    $.ajax({
                        url: "/Common/QueryOrderNos?noType=" + noType + "&workshopCode=" + $("#selWorkshops").val().trim() + "&key=" + "&startTime=" + startTime + "&endTime=" + endTime,
                        type: "GET",
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            if (noType == 0) {
                                txtOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            } else {
                                txtWorkOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            }
                        }
                    });
                }

                var options = {
                    elem: '#table'
                    , url: '/Report/QueryViaSiteRecords'
                    , method: "post"
                    , where: { workshopCode: "", lineId: 0, siteType: null, orderNo: "", workOrderNo: "", barCode: "", startTime: "@(DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd HH:mm:ss"))", endTime: "@(DateTime.Today.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"))" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: "left" }
                        , { field: 'Code', title: '组件条码', minWidth: 220, fixed: "left" }
                        , { field: 'CNSiteType', title: '过站类型', minWidth: 100, fixed: "left" }
                        , { field: 'MachineName', title: '过站设备', minWidth: 120, fixed: "left" }
                        , { field: 'CreateTimeStr', title: '过站时间', minWidth: 150 }
                        , { field: 'DutyName', title: '生产班次', minWidth: 100 }
                        , { field: 'OrderNo', title: '订单号', minWidth: 120 }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 200 }
                        , { field: 'TopPosition', title: '层压位置', minWidth: 100 }
                        , {
                            title: '判定等级', width: 100, templet: function (d) {
                                return [30, 50, 100, 120].indexOf(d.SiteType) > -1 ? d.QLevel : "";
                            }
                        }
                        , { field: 'BadReason', title: '异常原因', minWidth: 100 }
                        , {
                            title: '异常数量', minWidth: 100, templet: function (d) {
                                return d.BadCount <= 0 ? "" : d.BadCount;
                            }
                        }
                        , {
                            title: '异常坐标', minWidth: 100, templet: function (d) {
                                if (d.BadCoordinates == '') {
                                    return "";
                                }
                                var badCoordinates = new Array();
                                var dots = JSON.parse(d.BadCoordinates);
                                for (var i = 0; i < dots.length; i++) {
                                    badCoordinates.push(dots[i].X + dots[i].Y);
                                }
                                return badCoordinates.join(", ");
                            }
                        }
                        , {
                            title: '操作', width: 120, fixed: "right", templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="detail(\'' + d.Code + '\')"><i class="layui-icon">&#xe614;</i>组件详情</button>';
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.workshopCode = $("#selWorkshops").val().trim();
                    options.where.lineId = parseInt($("#selLines").val().trim());
                    options.where.siteType = $("#selSiteTypes").val().trim() == "" ? null : parseInt($("#selSiteTypes").val().trim());
                    options.where.orderNo = $("#txtOrderNo").val().trim();
                    options.where.workOrderNo = $("#txtWorkOrderNo").val().trim();
                    options.where.barCode = $("#txtBarCode").val().trim();
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    options.where.startTime = rangeTime.length <= 0 ? null : rangeTime[0];
                    options.where.endTime = rangeTime.length <= 0 ? null : rangeTime[1];

                    var params = new Array();
                    for (var item in options.where) {
                        params.push(item + "=" + options.where[item]);
                    }
                    $("#btnExport").attr("href", "/Report/ExportViaSiteRecords?" + params.join("&"));

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function detail(barCode) {
            parent.layui.index.openTabsPage("/Report/ComponentDetail?barCode=" + barCode, "组件详情(" + barCode + ")");
        }
    </script>
}
